import java.util.Arrays;
import java.util.Stack;

public class MyStack {
    public int[] array;
    public int size;   //实时更新
    static final int ELEMSIZE = 5;

    public MyStack() {
        this.array = new int[ELEMSIZE];
        this.size = 0;
    }
//    MyStack1 stack = new MyStack1();

    public void display(){
        //循环
        while(this.size() != 0){
            System.out.print(this.pop()+" ");
        }
        System.out.println();
    }

    public int push(int e) {
        //判断栈满没有
        ensureCapacity();
        this.array[this.size++] = e;
        return e;
    }

    public int pop() {
        //空栈问题
        return this.array[--this.size];  // 地址  size减一
    }

    public int pop1(){
        int e = peek();
        size--;
        return e;
    }

    public int peek() {
        //空栈问题

        return this.array[size()];
    }

    public int size() {
        return this.size - 1;
    }

    public boolean empty() {
        if (this.size == 0) {
            return true;
        }
        return false;
    }

    private void ensureCapacity() {
        //判断是否需要扩容
        if (this.size == this.array.length) {
            this.array = Arrays.copyOf(this.array, this.size * 2);
            System.out.println("扩容成功！");
        }
    }
}
